package cn.doctorpda.conference.ui.personal.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.Entity;
import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.bean.User;
import cn.doctorpda.conference.presenter.LoginPresenter;
import cn.doctorpda.conference.utils.MyTextUtil;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.utils.T;
import cn.doctorpda.conference.view.callbackview.IBaseView;
import cn.doctorpda.conference.view.callbackview.ILoginView;
import okhttp3.Call;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 */
public class LoginDialogFragment extends DialogFragment implements
        OnClickListener, ILoginView<User> {

    private FragmentActivity mActivity;
    private LoginPresenter presenter;
    private EditText etUserName;
    private EditText etPassWord;
    private OnLoginListener listener;

    public void setOnLoginListener(OnLoginListener listener) {
        this.listener = listener;
    }

    public interface OnLoginListener {
        void setLoginListener(Msg msg, User user);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mActivity = getActivity();
        View inflate = inflater.inflate(R.layout.fragment_login, null);
        inflate.findViewById(R.id.btn_login).setOnClickListener(this);
        inflate.findViewById(R.id.btn_close).setOnClickListener(this);
        inflate.findViewById(R.id.btn_to_register).setOnClickListener(this);
        inflate.findViewById(R.id.btn_msg_log).setOnClickListener(this);
        inflate.findViewById(R.id.btn_get_psw).setOnClickListener(this);
        etUserName = (EditText) inflate.findViewById(R.id.et_login_number);
        etPassWord = (EditText) inflate.findViewById(R.id.et_login_psw);
        getDialog().getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//        getDialog().setCanceledOnTouchOutside(false);
        return inflate;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter = new LoginPresenter(this);
    }

    @Override
    public void onClick(View v) {
//        dismiss();
        String mUserName = etUserName.getText().toString().trim();
        String mPassWord = etPassWord.getText().toString().trim();
        switch (v.getId()) {
            case R.id.btn_login:

                if (mUserName.isEmpty()) {
                    T.showShort(getContext(), "手机号不能为空");
                    break;
                }
                if (!MyTextUtil.isPhoneNumber(mUserName)) {
                    T.showShort(getContext(), "手机号不正确");
                    break;
                }
                if (!MyTextUtil.checkPassword(mPassWord)) {
                    T.showShort(getContext(), "密码格式不对");
                    break;
                }
                if (mPassWord.isEmpty()) {
                    T.showShort(getContext(), "密码不能为空");
                    break;
                }

                presenter.getLoginData(mUserName, mPassWord);
                break;
            case R.id.btn_to_register:
                RegisterDialogFragment rDialogFragment = new RegisterDialogFragment();
                rDialogFragment.show(getActivity().getSupportFragmentManager(), null);
                break;

            case R.id.btn_close:
                this.dismiss();
                break;
            case R.id.btn_msg_log:
                LoginWithVerifyCodeFragment loginWithVerifyCodeFragment = new LoginWithVerifyCodeFragment();
                loginWithVerifyCodeFragment.show(getActivity().getSupportFragmentManager(), null);
                this.dismiss();
                break;

            case R.id.btn_get_psw:
                RetrivePswDialogFragment retrivePswDialogFragment = new RetrivePswDialogFragment();
                retrivePswDialogFragment.show(getActivity().getSupportFragmentManager(), null);
                break;
            default:
                break;
        }
    }


    @Override
    public void setLoginResult(Msg msg, User data) {
//        T.showShort(getContext(), msg.getMsg());
        if (msg.isSuccess()) {
            this.dismiss();
        } else {
//            T.showShort(getContext(), msg.getMsg());
        }
        listener.setLoginListener(msg, data);
    }

    @Override
    public void setILoginToast(Msg msg) {
        if (!msg.getStatus().equals("success")) {
            L.d("这儿");
            T.showShort(getContext(), msg.getMsg());
        }
    }

    @Override
    public void setLoginError(Call call, Exception e) {
        T.showShort(getContext(), "登陆失败请稍后重试");
    }
}
